//
//  HLUpdateUserNameController.m
//  HLAerospace
//
//  Created by hulian-ios on 17/9/20.
//  Copyright © 2017年 hulian-LB. All rights reserved.
//

#import "HLUpdateUserNameController.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import "MBProgressHUD+HL.h"
#import "HttpRequestManager.h"
#import "HLUserInfo.h"

@interface HLUpdateUserNameController ()
/** 发布按钮  */
@property (nonatomic ,strong) UIButton *publishButton;
/** 标题  */
@property (nonatomic ,strong) UITextField *titleTextFiled;

@end

@implementation HLUpdateUserNameController

- (UIButton *)publishButton
{
    if (!_publishButton) {
        _publishButton = [[UIButton alloc] init];
        _publishButton.enabled = NO;
        [_publishButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
        [_publishButton setTitleColor:kColorMineBlueBG forState:UIControlStateNormal];
        [_publishButton setTitle:@"保存" forState:UIControlStateNormal];
        [_publishButton addTarget:self action:@selector(publishButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_publishButton sizeToFit];
    }
    return _publishButton;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNavItem];
    
    [self setUI];
    
    RAC(_publishButton,enabled) = [RACSignal combineLatest:@[self.titleTextFiled.rac_textSignal] reduce:^id _Nullable(NSString *content){
        return @( ![content isEqualToString:self.userName] );
    }];
}

- (void)setNavItem
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"修改用户名";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.publishButton];
    self.view.backgroundColor = HLRGB(245,245, 245);
}

- (void)setUI
{
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0,74, ScreenWidth, 44)];
    contentView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:contentView];
    
    self.titleTextFiled = [[UITextField alloc]init];
    self.titleTextFiled.textColor = HLRGB(30, 30, 30);
    self.titleTextFiled.font = [UIFont systemFontOfSize:16];
    self.titleTextFiled.frame = CGRectMake(10, 5, ScreenWidth - 20, 34);
    self.titleTextFiled.backgroundColor = [UIColor whiteColor];
    self.titleTextFiled.text = self.userName;
    [contentView addSubview:self.titleTextFiled];
}

- (void)publishButtonClick
{
    [self questionRequest];
}

- (BOOL)becomeFirstResponder
{
    [super becomeFirstResponder];
    return [self.titleTextFiled becomeFirstResponder];
}

- (void)questionRequest
{
    if (self.titleTextFiled.text.length == 0) {
        [MBProgressHUD showTipMessageInView:@"用户名不能为空"];
        return;
    }
    
    [MBProgressHUD show];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"sign"] = [kHLRequestChangeNickname signStr];
    params[@"user_id"] = [HLUserInfo account].userid;
    params[@"nickname"] = self.titleTextFiled.text;
    __weak typeof(self) weakSelf = self;
    [[HTTPRequestManager defaultManager] HTTPGetRequestWithApiName:kHLRequestChangeNickname parameters:params success:^(NSURLSessionDataTask * _Nonnull dataTask, id  _Nonnull responseObject){
        [MBProgressHUD hideHUD];
        if ([responseObject[@"code"] integerValue] == 200) {
            [MBProgressHUD showSuccessMessage:@"保存成功"];
            !weakSelf.complainBlock ? : weakSelf.complainBlock(weakSelf.titleTextFiled.text);
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nonnull dataTask, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showErrorMessage:@"网络超时"];
    }];
}



@end

